package com.mm.ai.service;


import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.mm.ai.config.AiProperties;
import com.mm.ai.domain.po.Coze4Response;
import com.mm.ai.domain.po.Gpt3Response;
import com.mm.ai.domain.po.Message;
import com.mm.common.domain.po.Result;
import com.mm.common.enums.ResultCode;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: 8.mindmap
 * @BelongsPackage: com.mm.ai.service
 * @Author : 游在海里的鱼
 * @CreateTime : 2024-05-17  16:28
 * @Version : 1.0
 * @ClassName : TODO
 * @Description : TODO
 */
@Data
@Service
@RequiredArgsConstructor
public class AiService {

    private final AiProperties aiProperties;
    private int count = 0;



    public Result chat3(String chat){
        List<Message> messages = new ArrayList<>();
        messages.add(new Message("user",chat));


        JSONObject jsonBody = JSONUtil.createObj()
                .put("model", aiProperties.default_chatgpt3_modal)
                .put("messages", messages)
                .put("temperature", aiProperties.default_temperature);

        HttpRequest request = HttpRequest
                .post(aiProperties.chatGPT3_host)
                .timeout(aiProperties.timeout)
                .setConnectionTimeout(aiProperties.connect_timeout)
                .setReadTimeout(aiProperties.read_timeout)
                .header("Authorization", "Bearer "+KeyRandomStrategy(aiProperties.chatgpt3_api_keys))
                .header("Content-Type", "application/json")
                .body(jsonBody.toString());

        HttpResponse response = request.execute();
        System.out.println(response.body());
        Gpt3Response gpt3Response = JSONUtil.toBean(response.body(), Gpt3Response.class);
        if(StrUtil.isBlank(gpt3Response.getContent())){
            if(count > aiProperties.repetition){
                count = 0;
                return Result.failed(ResultCode.FAILED,null);
            }
            count++;
            try {
                Thread.sleep(20000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            return chat3(chat);
        }

        return Result.success(gpt3Response.getContent());
    }

    public Result chat4(String chat){
        HttpRequest request = HttpRequest
                .post(aiProperties.coze4_host)
                .header("Authorization", "Bearer " + aiProperties.coze4_api_key)
                .form("conversation_id", aiProperties.coze4_conversation_id)
                .form("bot_id", aiProperties.coze4_bot_id)
                .form("user", "user")
                .form("query", chat)
                .form("stream", "false");

        HttpResponse response = request.execute();
        Coze4Response coze4Response = JSONUtil.toBean(response.body(), Coze4Response.class);
        return Result.success(coze4Response.getContent());
    }

    private String KeyRandomStrategy(List<String>keys){
        int index = RandomUtil.randomInt(keys.size());
        return keys.get(index);
    }
}
